<template>
  <div class="view-fill">
    <div
      v-if="!modifyInfo.visible && !assetDetailInfo.visible"
      class="app-container-flex"
    >
      <NewFullScreen
        v-model="showDialog"
        title="巡检记录登记详情"
        :needOperateAffair="false"
        :operateAffairData="affairTableInfo"
        :needReturn="!$route.query.processInstanceId"
        @returnBack="returnBack"
      >
        <template v-slot:full-button>
          <el-button
            @click="exportExcel"
            v-if="$permission('patrolRecordRegistration:excelDetails')"
            >导出excel</el-button
          >
          <PopoverPrint
            ref="popoverShow"
            v-model:popoverVisible="popoverVisible"
            :templateArr="templateArr"
            :canPrint="$permission('patrolRecordRegistration:printDetails')"
            @tempClick="tempClick"
            @print="print"
          ></PopoverPrint>
         
          <el-button v-if="$permission('patrolRecordRegistration:update')" @click="update">编辑</el-button>
         
        </template>
        <div class="model-panel">
          <div class="model-panel-title">
            <p>{{ $t('basic_information') }}</p>
          </div>
          <div class="model-panel-body">
            <basic-information :info="infoList" />
          </div>
        </div>

        <div class="model-panel">
          <div class="model-panel-title">
            <Tooltip :text="$t('detailInfo')" />
          </div>
          <div class="model-panel-body">
            <div class="flex_full_item">
              <!-- 表格 -->
              <page-table
                :refresh="tableInfo.refresh"
                :init-curpage="tableInfo.initCurpage"
                v-model:data="tableInfo.data"
                :pager="false"
                :check-box="false"
                :tab-index="true"
                :field-list="tableInfo.fieldList"
                :isDynamic="true"
              >
                <template v-slot:col-assetName="scope">
                  <!-- class="slip-edit" @click="showAssetDetail(scope.row)" -->
                  <span >{{
                    scope.row.assetName
                  }}</span>
                </template>
              </page-table>
            </div>
          </div>
        </div>
      </NewFullScreen>
    </div>
    <!-- 修改 -->
    <asset-transfer-modify
      v-if="modifyInfo.visible"
      v-model:visible="modifyInfo.visible"
      :title="modifyInfo.title"
      :id="modifyInfo.id"
    />
    <!-- 资产明细 -->
    <asset-card-detail
      v-if="assetDetailInfo.visible"
      :id="assetDetailInfo.id"
      v-model:visible="assetDetailInfo.visible"
      :title="assetDetailInfo.title"
    />
  </div>
</template>

<script setup>
import {
  getPatrolRecoredById,
  excelrecoredStatementOrderList,
} from '@/api/patrol.js'
import { getCustomizeSetting } from '@/api/customize'
import { orderOpAffairAll } from '@/api/affair'
import AssetTransferModify from './modify'
import { doPrint } from '@/utils/exportUtil.js'
import { selectByCodeAndDept } from '@/api/report.js'

const AssetCardDetail = defineAsyncComponent(() =>
  import('@/views/asset/assetManage/assetCard/details')
)

const { proxy } = getCurrentInstance()
const emits = defineEmits()
const props = defineProps(['visible', 'tableRow', 'id'])

const infoList = ref({
  data: {},
  fieldList: [
    { label: '使用部门', value: 'useDeptName', type: 'text' },
    { label: '部门人员', value: 'deptUserName', type: 'text' },
    { label: '巡检人员', value: 'patrolUserName', type: 'text' },
    { label: '巡检时间', value: 'patrolDate', type: 'text' },
    { label: '备注', value: 'remark', type: 'popover' },
    { label: '存在问题', value: 'existProblem', type: 'popover' },
    { label: '整改意见和措施', value: 'suggestion', type: 'popover' },
  ],
})
// 过滤相关配置
const filterInfo = ref({
  query: {
    customizeUserVO: {},
    queryMap: {
      orderId: {
        queryType: 'EQUALS',
        queryCategory: 'AND',
        value: props.id || props.tableRow.recoredId,
      },
    },
    queryParams: {},
  },
})
const modifyInfo = ref({
  visible: false,
  id: '',
  title: '',
})
// 表格相关
const tableInfo = ref({
  refresh: 1,
  initCurpage: 1,
  data: [],
  fieldList: [],
})
const assetDetailInfo = ref({
  visible: false,
  id: '',
  title: '详情',
})
const affairTableInfo = ref({
  api: orderOpAffairAll,
  query: {
    //数字字典单据类型 资产转移：45
    orderType: 45,
    orderId: props.id || props.tableRow.recoredId,
    sort: 'operate_time desc',
  },
})

const showDialog = computed({
  get() {
    return props.visible
  },
  set(newValue) {
    emits('update:visible', newValue) // 更新父组件visible
  },
})

watch(
  () => props.visible,
  (value) => {
    showDialog.value = value
  },
  {
    immediate: true,
  }
)
watch(
  () => modifyInfo.value.visible,
  (value) => {
    if (!value) {
      getMain()
    }
  }
)

setData()
function setData() {
  let obj = {
    patrolRecoredDetailResult: {
      label: "巡检结果",
      value: "patrolRecoredDetailResult",
      type: "text",
      show: false,
      hidden: true,
    },
  }
  tableInfo.value.fieldList = Object.values({
    ...proxy.$fn.deepClone(proxy.$fieldfList.FieldList),
    ...proxy.$fn.deepClone(obj),
  })
  getCustomize()
}
function showAssetDetail(row) {
  assetDetailInfo.value.id = row.assetId
  assetDetailInfo.value.visible = true
}


function update() {
  modifyInfo.value.id = infoList.value.data.id
  modifyInfo.value.title = '编辑'
  modifyInfo.value.visible = true
}
function returnBack() {
  emits('returnBack', false)
}

function getMain() {
  console.log(props.tableRow)
  getPatrolRecoredById(props.id || props.tableRow.recoredId).then((resp) => {
    if (resp && resp.success) {
      infoList.value.data = resp.data
      tableInfo.value.data = resp.data.patrolRecoredEditDetailList
    }
  })
}

//导出excel
async function exportExcel() {
  excelrecoredStatementOrderList(filterInfo.value.query).then((resp) => {
    if (resp) {
      proxy.$fn.parseExcel(resp)
    }
  })
}

const templateArr = ref([])
const popoverVisible = ref(false)
//打印
function print() {
  selectByCodeAndDept({
    tempCode: 'patrolRecoredStatement',
    // recordId: infoList.value.data.id,
  }).then((res) => {
    if (res && res.success) {
      templateArr.value = res.data
      popoverVisible.value = true
      // 一条数据则直接打印
      if (templateArr.value.length === 1) {
        proxy.$refs.popoverShow.hidePopover()
        doPrint({
          name: templateArr.value[0].ureportName,
          params: {
            // applicationId: infoList.value.data.id,
            recordId: infoList.value.data.id,
            // orderNo: infoList.value.data.orderNo,
          },
          callback: printCallback,
          data: {},
        })
      }
    }
  })
}
function printCallback() {}
function tempClick(item) {
  doPrint({
    name: item.ureportName,
    params: {
      applicationId: infoList.value.data.id,
      orderId: infoList.value.data.id,
      orderNo: infoList.value.data.orderNo,
    },
    callback: printCallback,
    data: {},
  })
  popoverVisible.value = false
}
function getCustomize() {
  let param = {
    tableKey:
      'patrolRecoredEditDetailVO-com.slip.spd.entity.vo.patrol.PatrolRecoredEditDetailVO',
  }
  getCustomizeSetting(param).then((res) => {
    if (res && res.success) {
      let data = res.data
      filterInfo.value.query.customizeUserVO = data
      let obj = proxy.$fn.setFieldList(
        tableInfo.value.fieldList,
        data.detail,
        true
      )
      tableInfo.value.fieldList = obj.fieldList
      getMain()
    }
  })
}
</script>

<style scoped></style>
